/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Telas;

import Gerenciar.GerenciarCliente;
import entidades.Cliente;
import java.util.List;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Deyves
 */
public class ConsultaCliente extends javax.swing.JInternalFrame {

    private List<Cliente> lista = GerenciarCliente.trazerClientes();

    public ConsultaCliente() {
        initComponents();
        atualizarTabelaCliente();
        btnLocalizar.setVisible(false);

    }
    private static ConsultaCliente instance = null;

    public static ConsultaCliente getInstance() {  ////////----------------- INICIA UMA NOVA INSTANCIA DA TELA
        if (instance == null) {
            instance = new ConsultaCliente();
        }
        return instance;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblConsultaCliente = new javax.swing.JTable();
        txtLocalizar = new javax.swing.JTextField();
        btnLocalizar = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setClosable(true);
        setTitle("CONSULTA DE CLIENTES");

        tblConsultaCliente.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "id", "Nome", "CPF", "Telefone", "Endereco"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblConsultaCliente.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(tblConsultaCliente);
        tblConsultaCliente.getColumnModel().getColumn(0).setMinWidth(0);
        tblConsultaCliente.getColumnModel().getColumn(0).setMaxWidth(0);
        tblConsultaCliente.getColumnModel().getColumn(1).setMinWidth(230);
        tblConsultaCliente.getColumnModel().getColumn(1).setMaxWidth(230);
        tblConsultaCliente.getColumnModel().getColumn(2).setMinWidth(120);
        tblConsultaCliente.getColumnModel().getColumn(2).setMaxWidth(120);
        tblConsultaCliente.getColumnModel().getColumn(3).setMinWidth(120);
        tblConsultaCliente.getColumnModel().getColumn(3).setMaxWidth(120);
        tblConsultaCliente.getColumnModel().getColumn(4).setMinWidth(400);
        tblConsultaCliente.getColumnModel().getColumn(4).setMaxWidth(400);

        txtLocalizar.setFont(new java.awt.Font("Arial", 2, 14)); // NOI18N
        txtLocalizar.setText("Digite aqui");
        txtLocalizar.addContainerListener(new java.awt.event.ContainerAdapter() {
            public void componentAdded(java.awt.event.ContainerEvent evt) {
                txtLocalizarComponentAdded(evt);
            }
        });
        txtLocalizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtLocalizarActionPerformed(evt);
            }
        });
        txtLocalizar.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtLocalizarFocusLost(evt);
            }
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtLocalizarFocusGained(evt);
            }
        });
        txtLocalizar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtLocalizarKeyPressed(evt);
            }
        });

        btnLocalizar.setText("Localizar");
        btnLocalizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLocalizarActionPerformed(evt);
            }
        });

        btnEditar.setText("Editar");
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 813, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtLocalizar, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnLocalizar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnEditar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnCancelar)
                .addGap(45, 45, 45))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtLocalizar, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE)
                    .addComponent(btnLocalizar)
                    .addComponent(btnEditar)
                    .addComponent(btnCancelar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 438, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtLocalizarFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtLocalizarFocusGained
        txtLocalizar.setText("");
    }//GEN-LAST:event_txtLocalizarFocusGained

    private void txtLocalizarFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtLocalizarFocusLost
        if (txtLocalizar.getText().trim().equalsIgnoreCase("")) {
            txtLocalizar.setText("Digite aqui");
        }
    }//GEN-LAST:event_txtLocalizarFocusLost

    private void txtLocalizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtLocalizarActionPerformed
    }//GEN-LAST:event_txtLocalizarActionPerformed

    private void btnLocalizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLocalizarActionPerformed
//       for (int i = 0; i <= tblConsultaCliente.getRowCount()-1; i++) {
//            if (((DefaultTableModel) tblConsultaCliente.getModel()).getValueAt(i, 1).toString().contains(txtLocalizar.getText().toUpperCase())) {
//                ((DefaultTableModel) tblConsultaCliente.getModel()).getValueAt(i, 1);
//                tblConsultaCliente.changeSelection(i, 1, closable, isIcon);
//            }else{
//                JOptionPane.showMessageDialog(null, "Não Encontrado!");
//                txtLocalizar.setText("Digite aqui");
//            }
//        }
    }//GEN-LAST:event_btnLocalizarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        txtLocalizar.setText("Digite aqui");
        tblConsultaCliente.clearSelection();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void txtLocalizarComponentAdded(java.awt.event.ContainerEvent evt) {//GEN-FIRST:event_txtLocalizarComponentAdded

    }//GEN-LAST:event_txtLocalizarComponentAdded

    private void txtLocalizarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtLocalizarKeyPressed
        for (int i = 0; i <= tblConsultaCliente.getRowCount() - 1; i++) {
            if (((DefaultTableModel) tblConsultaCliente.getModel()).getValueAt(i, 1).toString().contains(txtLocalizar.getText().toUpperCase())) {
                ((DefaultTableModel) tblConsultaCliente.getModel()).getValueAt(i, 1);
                tblConsultaCliente.changeSelection(i, 1, closable, isIcon);
            } else {
//                JOptionPane.showMessageDialog(null, "Não Encontrado!");
//                txtLocalizar.setText("Digite aqui");
            }
        }
    }//GEN-LAST:event_txtLocalizarKeyPressed

    private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
        if (tblConsultaCliente.getSelectedRow() >= 0) {  //----------- CONDIÇÃO EM QUE SE TIVER UMA LINHA SELECIONA, ENTAO EXECUTA 
            //Cliente cliente = new Cliente();
            int id = (int) tblConsultaCliente.getValueAt(tblConsultaCliente.getSelectedRow(), 0);
            //List<Cliente> cliente =  GerenciarCliente.trazerCliente(id);
            Cliente cliente1 = GerenciarCliente.trazerCliente(id);
        }
    }//GEN-LAST:event_btnEditarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnLocalizar;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblConsultaCliente;
    private javax.swing.JTextField txtLocalizar;
    // End of variables declaration//GEN-END:variables

    private void atualizarTabelaCliente() { //--------- METODO DE ATUALIZAR TABELA
        limparTabela();
        //List<Cliente> lista = GerenciarCliente.trazerClientes();
        String juntaCliente;
        Object[] objectemp = new Object[5];
        for (int i = 0; i <= lista.size() - 1; i++) {
            objectemp[0] = lista.get(i).getId();
            juntaCliente = lista.get(i).getNome();
            objectemp[1] = juntaCliente.toUpperCase();
            System.out.println(objectemp[1]);
            juntaCliente = lista.get(i).getCpf().toString();
            objectemp[2] = juntaCliente.toUpperCase();
            objectemp[3] = null;
            juntaCliente = lista.get(i).getEnderecocliente().getRua() + ", "
                    + lista.get(i).getEnderecocliente().getNumero().toString() + " - "
                    + lista.get(i).getEnderecocliente().getBairro() + " / "
                    + lista.get(i).getEnderecocliente().getCidade().getNome() + " - "
                    + lista.get(i).getEnderecocliente().getCidade().getEstado().getDescricao();
            objectemp[4] = juntaCliente.toUpperCase();
            ((DefaultTableModel) tblConsultaCliente.getModel()).addRow(objectemp);
        }
    }

    private void limparTabela() { //---------- LIMPAR TABELA
        int numLinhas = ((DefaultTableModel) tblConsultaCliente.getModel()).getRowCount();
        for (int i = numLinhas - 1; i >= 0; i--) {
            ((DefaultTableModel) tblConsultaCliente.getModel()).removeRow(i);
        }
    }
}
